
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>单元测试 · 启路框架-使用手册</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-plus/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-insert-logo/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-toggle-chapters/toggle.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-lightbox/css/lightbox.min.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-emphasize/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-prism/prism.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-flexible-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-page-toc-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-donate/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    


    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="function.html" />
    
    
    <link rel="prev" href="readme.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    整体介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../ready/ready.html">
            
                <a href="../ready/ready.html">
            
                    
                    技术准备
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../ready/ide.html">
            
                <a href="../ready/ide.html">
            
                    
                    eclipse
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../ready/build.html">
            
                <a href="../ready/build.html">
            
                    
                    本地构建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../ready/nexus.html">
            
                <a href="../ready/nexus.html">
            
                    
                    私服构建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../ready/run.html">
            
                <a href="../ready/run.html">
            
                    
                    运行调试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../ready/specification.html">
            
                <a href="../ready/specification.html">
            
                    
                    结构和分包
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../base/baseReadme.html">
            
                <a href="../base/baseReadme.html">
            
                    
                    基础包介绍
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../base/qiluTask-readme.html">
            
                <a href="../base/qiluTask-readme.html">
            
                    
                    qilu-task
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../base/cache-readme.html">
            
                <a href="../base/cache-readme.html">
            
                    
                    qilucache
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../base/qiluMybatis-readme.html">
            
                <a href="../base/qiluMybatis-readme.html">
            
                    
                    qilu-mybatis
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../base/logTrace-readme.html">
            
                <a href="../base/logTrace-readme.html">
            
                    
                    qilu-logTrace
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../base/qilu-export-readme.html">
            
                <a href="../base/qilu-export-readme.html">
            
                    
                    qilu-export
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../base/httpService-readme.html">
            
                <a href="../base/httpService-readme.html">
            
                    
                    qilu-http-service
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../base/validateReadme.html">
            
                <a href="../base/validateReadme.html">
            
                    
                    qilu-validate
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../state/state-course.html">
            
                <a href="../state/state-course.html">
            
                    
                    交易一致的由来
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../state/state-settle.html">
            
                <a href="../state/state-settle.html">
            
                    
                    qilu-state使用说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../state/state-demo.html">
            
                <a href="../state/state-demo.html">
            
                    
                    购买订单的处理示例
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../backSample/backSampleReadme.html">
            
                <a href="../backSample/backSampleReadme.html">
            
                    
                    后台代码模板
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../backSample/security.html">
            
                <a href="../backSample/security.html">
            
                    
                    安全介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../backSample/deploy.html">
            
                <a href="../backSample/deploy.html">
            
                    
                    部署集成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../backSample/config.html">
            
                <a href="../backSample/config.html">
            
                    
                    spring配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../backSample/sso.html">
            
                <a href="../backSample/sso.html">
            
                    
                    SSO集成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../backSample/example.html">
            
                <a href="../backSample/example.html">
            
                    
                    常见代码示例
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="readme.html">
            
                <a href="readme.html">
            
                    
                    关于测试
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.6.1" data-path="unitTest.html">
            
                <a href="unitTest.html">
            
                    
                    单元测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="function.html">
            
                <a href="function.html">
            
                    
                    功能测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="noFunction.html">
            
                <a href="noFunction.html">
            
                    
                    非功能测试
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../video/test.html">
            
                <a href="../video/test.html">
            
                    
                    视频
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../video/build.html">
            
                <a href="../video/build.html">
            
                    
                    项目构建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../video/state.html">
            
                <a href="../video/state.html">
            
                    
                    交易一致
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >单元测试</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div class="search-plus" id="book-search-results">
    <div class="search-noresults">
    
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x5355;&#x5143;&#x6D4B;&#x8BD5;">&#x5355;&#x5143;&#x6D4B;&#x8BD5;</h1>
<p>&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x5C5E;&#x4E8E;&#x5F00;&#x53D1;&#x6D4B;&#x8BD5;,&#x4E3B;&#x8981;&#x662F;&#x5728;&#x6784;&#x5EFA;&#x8FC7;&#x7A0B;&#x4E2D;,&#x6267;&#x884C;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;,&#x5224;&#x65AD;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x7684;&#x67D0;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x6216;&#x65B9;&#x6CD5;,&#x5728;&#x7279;&#x5B9A;&#x7684;&#x8F93;&#x5165;/&#x6570;&#x636E;&#x60C5;&#x51B5;&#x4E0B;&#x7684;&#x8FD4;&#x56DE;.</p>
<p>&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x540C;&#x6837;&#x5C5E;&#x4E8E;&#x81EA;&#x52A8;&#x5316;&#x6D4B;&#x8BD5;&#x7684;&#x8303;&#x7574;.&#x540C;&#x6837;&#x9700;&#x8981;&#x6EE1;&#x8DB3;&#x9690;&#x85CF;&#x6761;&#x4EF6;,&#x4EFB;&#x4F55;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x90FD;&#x53EF;&#x4EE5;&#x88AB;&#x91CD;&#x590D;&#x6267;&#x884C;.</p>
<p>&#x6240;&#x4EE5;,&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x6700;&#x4E3B;&#x8981;&#x7684;&#x5DE5;&#x4F5C;&#x91CF;&#x5C31;&#x5728;&#x4E8E;,&#x6784;&#x5EFA;&#x6D4B;&#x8BD5;&#x73AF;&#x5883;&#x548C;&#x6D4B;&#x8BD5;&#x6570;&#x636E;.</p>
<h1 id="&#x6846;&#x67B6;&#x548C;&#x65B9;&#x6CD5;">&#x6846;&#x67B6;&#x548C;&#x65B9;&#x6CD5;</h1>
<p>&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x7684;&#x4E3B;&#x8981;&#x6846;&#x67B6;---junit&#x548C;testNG,&#x4E8C;&#x8005;&#x90FD;&#x80FD;&#x88AB;&#x6784;&#x5EFA;&#x5DE5;&#x5177;&#x8BC6;&#x522B;&#x548C;&#x6267;&#x884C;.&#x4E8C;&#x8005;&#x4E3B;&#x8981;&#x7684;&#x533A;&#x522B;&#x5728;&#x4E8E;,testNG&#x53EF;&#x4EE5;&#x7F16;&#x6392;&#x987A;&#x5E8F;.</p>
<p>&#x8FD9;&#x8FB9;&#x4EE5;junit&#x6765;&#x8BF4;&#x660E;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x5B9E;&#x73B0;&#x624B;&#x6BB5;.</p>
<h1 id="ide&#x96C6;&#x6210;&#x4EE3;&#x7801;&#x8986;&#x76D6;&#x7387;">IDE&#x96C6;&#x6210;&#x4EE3;&#x7801;&#x8986;&#x76D6;&#x7387;</h1>
<p>&#x8FD9;&#x8FB9;&#x4EE5;eclipse&#x4E3A;&#x4E3B;,&#x63A8;&#x8350;&#x5404;&#x4F4D;&#x4E00;&#x4E2A;eclipse&#x63D2;&#x4EF6;---<a href="https://www.eclemma.org/" target="_blank">EclEmma</a></p>
<p>&#x5B89;&#x88C5;&#x5B8C;&#x6210;&#x4EE5;&#x540E;,&#x5728;eclipse&#x7684;&#x6267;&#x884C;&#x4F1A;&#x51FA;&#x73B0;&#x5982;&#x4E0B;&#x56FE;&#x6807;</p>
<p><a href="eclma1.png" data-lightbox="8440e74f-13a4-4732-b8ed-8dc84445291b" data-title="&#x6267;&#x884C;&#x63D2;&#x4EF6;"><img src="eclma1.png" alt="&#x6267;&#x884C;&#x63D2;&#x4EF6;"></a></p>
<p>&#x4F7F;&#x7528;&#x8BE5;&#x56FE;&#x6807;&#x6267;&#x884C;junit&#x7684;&#x5355;&#x5143;&#x6D4B;&#x8BD5;
<a href="eclma.png" data-lightbox="5abbf82c-a674-4bef-923d-211a30fe7c5c" data-title="&#x6267;&#x884C;&#x6548;&#x679C;"><img src="eclma.png" alt="&#x6267;&#x884C;&#x6548;&#x679C;"></a></p>
<p>&#x767E;&#x5206;&#x6BD4;&#x5C31;&#x662F;&#x4EE3;&#x7801;&#x8986;&#x76D6;&#x7387;,&#x540E;&#x9762;&#x662F;&#x6267;&#x884C;&#x7684;&#x884C;&#x6570;&#x548C;&#x4EE3;&#x7801;&#x7684;&#x884C;&#x6570;.</p>
<blockquote>
<p>[!WARNING]
&#x4E3A;&#x4EC0;&#x4E48;eclemma&#x7684;&#x4EE3;&#x7801;&#x8986;&#x76D6;&#x7387;&#x548C;Sonar&#x4E0A;&#x7684;&#x4E0D;&#x4E00;&#x6837;?
&#x4E3B;&#x8981;&#x662F;&#x8BA1;&#x7B97;&#x65B9;&#x6CD5;&#x4E0D;&#x4E00;&#x6837;,sonar&#x7684;&#x6267;&#x884C;&#x662F;&#x9488;&#x5BF9;&#x6574;&#x4E2A;&#x6784;&#x5EFA;&#x6267;&#x884C;&#x7684;,eclemma&#x7684;&#x6267;&#x884C;&#x6700;&#x591A;&#x662F;&#x6309;&#x7167;eclipse&#x5DE5;&#x7A0B;&#x6765;&#x8BA1;&#x7B97;&#x7684;.</p>
</blockquote>
<h1 id="&#x6570;&#x636E;&#x5E93;&#x7684;&#x5355;&#x5143;&#x6D4B;&#x8BD5;">&#x6570;&#x636E;&#x5E93;&#x7684;&#x5355;&#x5143;&#x6D4B;&#x8BD5;</h1>
<p>&#x6570;&#x636E;&#x5E93;&#x7684;&#x6570;&#x636E;&#x5BF9;&#x4E8E;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x6765;&#x8BF4;,&#x4E0D;&#x80FD;mock,&#x4E00;&#x5B9A;&#x8981;&#x505A;&#x771F;&#x5B9E;&#x7684;&#x6570;&#x636E;&#x64CD;&#x4F5C;,&#x5305;&#x62EC;&#x63D2;&#x5165;,&#x4FEE;&#x6539;&#x548C;&#x5220;&#x9664;.</p>
<pre class="language-"><code class="lang-java"><span class="token comment">//&#x4F7F;&#x7528;spring&#x8FD0;&#x884C;junit</span>
<span class="token annotation punctuation">@RunWith</span><span class="token punctuation">(</span><span class="token class-name">SpringJUnit4ClassRunner</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span>
<span class="token comment">//&#x52A0;&#x8F7D;&#x7279;&#x5B9A;&#x7684;spring&#x914D;&#x7F6E;&#x6587;&#x4EF6;</span>
<span class="token annotation punctuation">@ContextConfiguration</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">&quot;classpath:test_spring-service.xml&quot;</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token comment">//&#x6240;&#x6709;&#x7684;&#x6D4B;&#x8BD5;&#x65B9;&#x6CD5;&#x90FD;&#x5177;&#x6709;&#x4E8B;&#x52A1;&#x5C5E;&#x6027;</span>
<span class="token annotation punctuation">@Transactional</span>
<span class="token comment">//&#x9ED8;&#x8BA4;&#x4E8B;&#x52A1;&#x56DE;&#x6EDA;,&#x5728;spring-test2.5&#x4EE5;&#x540E;,&#x9700;&#x8981;&#x4F7F;&#x7528;@Rollback&#x4EE3;&#x66FF;</span>
<span class="token annotation punctuation">@TransactionConfiguration</span><span class="token punctuation">(</span>defaultRollback<span class="token operator">=</span><span class="token boolean">true</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">AppDemoServiceTest</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">OrgUserService</span> userService<span class="token punctuation">;</span>
    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">addOrgUserTest</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span><span class="token punctuation">{</span>
        ou<span class="token punctuation">.</span><span class="token function">setIdCode</span><span class="token punctuation">(</span><span class="token string">&quot;111&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        ou<span class="token punctuation">.</span><span class="token function">setPassword</span><span class="token punctuation">(</span><span class="token string">&quot;123&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        ou<span class="token punctuation">.</span><span class="token function">setUserCode</span><span class="token punctuation">(</span><span class="token string">&quot;111&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        ou<span class="token punctuation">.</span><span class="token function">setUserName</span><span class="token punctuation">(</span><span class="token string">&quot;abc&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        ou<span class="token punctuation">.</span><span class="token function">setDeptCode</span><span class="token punctuation">(</span><span class="token string">&quot;111&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        ou<span class="token punctuation">.</span><span class="token function">setRemark</span><span class="token punctuation">(</span><span class="token string">&quot;111&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> ret <span class="token operator">=</span> userService<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>ou<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Assert</span><span class="token punctuation">.</span><span class="token function">assertTrue</span><span class="token punctuation">(</span><span class="token class-name">StringUtils</span><span class="token punctuation">.</span><span class="token function">isNotBlank</span><span class="token punctuation">(</span>ret<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h1 id="&#x8FDC;&#x7A0B;&#x65B9;&#x6CD5;&#x7684;&#x6A21;&#x62DF;">&#x8FDC;&#x7A0B;&#x65B9;&#x6CD5;&#x7684;&#x6A21;&#x62DF;</h1>
<p>&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x7684;&#x6267;&#x884C;&#x4E0D;&#x5E94;&#x8BE5;&#x4F9D;&#x8D56;&#x4E8E;&#x8FDC;&#x7A0B;&#x7CFB;&#x7EDF;&#x7684;&#x542F;&#x505C;.&#x6240;&#x4EE5;&#x9488;&#x5BF9;&#x8FDC;&#x7A0B;&#x65B9;&#x6CD5;,&#x6211;&#x4EEC;&#x9700;&#x8981;&#x4F7F;&#x7528;mock.</p>
<p>&#x56E0;&#x4E3A;spring&#x6CA1;&#x6709;&#x63D0;&#x4F9B;&#x65B9;&#x4FBF;&#x7684;&#x6D4B;&#x8BD5;&#x66FF;&#x4EE3;,&#x8FD9;&#x8FB9;&#x6709;2&#x79CD;&#x65B9;&#x6CD5;&#x9009;&#x62E9;</p>
<ol>
<li>&#x4E0D;&#x626B;&#x63CF;&#x8FDC;&#x7A0B;&#x5305;,&#x4F7F;&#x7528;xml&#x6216;&#x8005;&#x4EE3;&#x7801;&#x65B9;&#x5F0F;,&#x6784;&#x5EFA;&#x7279;&#x6B8A;&#x7684;&#x8FDC;&#x7A0B;&#x65B9;&#x6CD5;&#x7C7B;,&#x8FDB;&#x884C;&#x6CE8;&#x5165;</li>
<li>&#x5728;@before&#x4E2D;,&#x4F7F;&#x7528;&#x53CD;&#x5C04;&#x65B9;&#x5F0F;,&#x66FF;&#x6362;&#x5230;&#x8FDC;&#x7A0B;&#x65B9;&#x6CD5;&#x7C7B;.</li>
</ol>
<pre class="language-"><code class="lang-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TargetClass</span> <span class="token punctuation">{</span>

    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">DubboService</span> remote<span class="token punctuation">;</span><span class="token comment">//&#x8FDC;&#x7A0B;&#x65B9;&#x6CD5;&#x7C7B;</span>

    <span class="token keyword">public</span> xx <span class="token function">a</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token comment">//&#x5B9E;&#x73B0;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x6D4B;&#x8BD5;&#x7C7B;&#x7684;&#x7F16;&#x5199;</p>
<pre class="language-"><code class="lang-java"><span class="token annotation punctuation">@RunWith</span><span class="token punctuation">(</span><span class="token class-name">SpringJUnit4ClassRunner</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@ContextConfiguration</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">&quot;classpath:application-testService.xml&quot;</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@Transactional</span>
<span class="token annotation punctuation">@TransactionConfiguration</span><span class="token punctuation">(</span>defaultRollback<span class="token operator">=</span><span class="token boolean">true</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestServiceTestCase</span> <span class="token punctuation">{</span>

    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">TargetService</span> service<span class="token punctuation">;</span>

    <span class="token annotation punctuation">@Before</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        service<span class="token punctuation">.</span>remote <span class="token operator">=</span> <span class="token function">mock</span><span class="token punctuation">(</span><span class="token class-name">DubboService</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test1</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span><span class="token punctuation">{</span>
        <span class="token comment">//&#x6267;&#x884C;service</span>
        service<span class="token punctuation">.</span><span class="token function">a</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h1 id="&#x7279;&#x6B8A;&#x53C2;&#x6570;&#x7684;&#x6A21;&#x62DF;">&#x7279;&#x6B8A;&#x53C2;&#x6570;&#x7684;&#x6A21;&#x62DF;</h1>
<p>&#x9488;&#x5BF9;&#x67D0;&#x4E9B;&#x65B9;&#x6CD5;&#x4E2D;&#x7684;&#x5165;&#x53C2;,&#x4F1A;&#x4F7F;&#x7528;&#x5230;HttpServletRequest&#x6216;&#x8005;HttpServletResponse&#x7B49;&#x505A;&#x4E3A;&#x53C2;&#x6570;.&#x8FD9;&#x7C7B;&#x53C2;&#x6570;&#x4E00;&#x822C;&#x901A;&#x8FC7;mock&#x7684;&#x65B9;&#x5F0F;&#x8FDB;&#x884C;&#x6CE8;&#x5165;</p>
<p>&#x6309;&#x7167;&#x7528;&#x5230;&#x54EA;&#x4E9B;&#x53C2;&#x6570;,mock&#x54EA;&#x4E9B;&#x53C2;&#x6570;&#x8FDB;&#x884C;&#x6A21;&#x62DF;.</p>
<pre class="language-"><code class="lang-java"><span class="token keyword">import</span> <span class="token keyword">static</span> <span class="token namespace">org<span class="token punctuation">.</span>mockito</span><span class="token punctuation">.</span><span class="token class-name">Mockito</span><span class="token punctuation">.</span>*<span class="token punctuation">;</span>

&#x4EE3;&#x7801;&#x4E2D;&#x8FDB;&#x884C;mock
<span class="token class-name">HttpServletRequest</span> request <span class="token operator">=</span> <span class="token function">mock</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">when</span><span class="token punctuation">(</span>request<span class="token punctuation">.</span><span class="token function">getAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;senvon&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">thenReturn</span><span class="token punctuation">(</span><span class="token string">&quot;senvon&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">when</span><span class="token punctuation">(</span>request<span class="token punctuation">.</span><span class="token function">getContextPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">thenReturn</span><span class="token punctuation">(</span><span class="token string">&quot;senvon&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<h1 id="&#x591A;&#x7EBF;&#x7A0B;&#x5355;&#x5143;&#x6D4B;&#x8BD5;">&#x591A;&#x7EBF;&#x7A0B;&#x5355;&#x5143;&#x6D4B;&#x8BD5;</h1>
<p>&#x67D0;&#x4E9B;&#x5173;&#x952E;&#x6027;&#x7684;&#x64CD;&#x4F5C;,&#x6BD4;&#x5982;&#x79D2;&#x6740;&#x7684;&#x65F6;&#x5019;&#x66F4;&#x6539;&#x5E93;&#x5B58;&#x64CD;&#x4F5C;,&#x4EA4;&#x6613;&#x7CFB;&#x7EDF;&#x4E2D;,&#x53D8;&#x66F4;&#x91D1;&#x989D;&#x7684;&#x64CD;&#x4F5C;,&#x9700;&#x8981;&#x5BF9;&#x591A;&#x7EBF;&#x7A0B;&#x7684;&#x73AF;&#x5883;&#x8FDB;&#x884C;&#x6D4B;&#x8BD5;.<strong style="color:red">&#x6D4B;&#x8BD5;&#x4EBA;&#x5458;&#x65E0;&#x6CD5;&#x5B8C;&#x5168;&#x6A21;&#x62DF;&#x8FD9;&#x79CD;&#x7C7B;&#x578B;&#x7684;&#x64CD;&#x4F5C;</strong></p>
<p>&#x591A;&#x7EBF;&#x7A0B;&#x7684;&#x73AF;&#x5883;&#x4E0B;,&#x6240;&#x6709;&#x7684;&#x4E8B;&#x52A1;&#x64CD;&#x4F5C;&#x90FD;&#x4F1A;&#x4E0D;&#x8D77;&#x4F5C;&#x7528;,&#x6240;&#x4EE5;&#x4E0D;&#x80FD;&#x4F7F;&#x7528;&#x5168;&#x5C40;&#x4E8B;&#x52A1;&#x56DE;&#x6EDA;&#x7684;&#x7B56;&#x7565;.&#x9700;&#x8981;&#x4F7F;&#x7528;@before&#x548C;@after&#x7684;&#x65B9;&#x5F0F;&#x521B;&#x5EFA;&#x6570;&#x636E;</p>
<pre class="language-"><code class="lang-java"><span class="token annotation punctuation">@Before</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span><span class="token punctuation">{</span>
        <span class="token comment">//&#x521B;&#x5EFA;&#x6570;&#x636E;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@After</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">after</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span><span class="token punctuation">{</span>
        <span class="token comment">//&#x9500;&#x6BC1;&#x6570;&#x636E;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">//&#x505A;&#x6D4B;&#x8BD5;</span>
    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test1</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span><span class="token punctuation">{</span>
        <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>count <span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token class-name">Thread</span> t <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">ExecuteThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            t<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            latch<span class="token punctuation">.</span><span class="token function">countDown</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        cb<span class="token punctuation">.</span><span class="token function">await</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//Thread.sleep(1000);</span>
        <span class="token comment">//&#x68C0;&#x67E5;&#x7ED3;&#x679C;</span>
        <span class="token class-name">Assert</span><span class="token punctuation">.</span><span class="token function">assertTrue</span><span class="token punctuation">(</span>&#x7ED3;&#x679C; <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">private</span> <span class="token keyword">int</span> count <span class="token operator">=</span> <span class="token number">100</span><span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">CountDownLatch</span> latch <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">CountDownLatch</span><span class="token punctuation">(</span>count<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">CyclicBarrier</span> cb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">CyclicBarrier</span><span class="token punctuation">(</span>count<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token keyword">class</span> <span class="token class-name">ExecuteThread</span> <span class="token keyword">implements</span> <span class="token class-name">Runnable</span><span class="token punctuation">{</span>

        <span class="token annotation punctuation">@Override</span>
        <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">try</span> <span class="token punctuation">{</span>
                latch<span class="token punctuation">.</span><span class="token function">await</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token comment">//&#x6D4B;&#x8BD5;&#x7684;&#x4E1A;&#x52A1;&#x903B;&#x8F91;</span>
            <span class="token comment">//do something</span>
            <span class="token keyword">try</span> <span class="token punctuation">{</span>
                cb<span class="token punctuation">.</span><span class="token function">await</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
</code></pre>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; senvon 2020 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">Bilibili,&#x5934;&#x6761;&#x53F7;,&#x5FAE;&#x4FE1;&#x53F7;&#xFF1A;&#x8BF7;&#x641C;&#x7D22;  &#x968F;senvon&#x98DE;
2021-10-08 14:51:27
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="readme.html" class="navigation navigation-prev " aria-label="Previous page: 关于测试">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="function.html" class="navigation navigation-next " aria-label="Next page: 功能测试">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"单元测试","level":"1.6.1","depth":2,"next":{"title":"功能测试","level":"1.6.2","depth":2,"path":"test/function.md","ref":"test/function.md","articles":[]},"previous":{"title":"关于测试","level":"1.6","depth":1,"path":"test/readme.md","ref":"test/readme.md","articles":[{"title":"单元测试","level":"1.6.1","depth":2,"path":"test/unitTest.md","ref":"test/unitTest.md","articles":[]},{"title":"功能测试","level":"1.6.2","depth":2,"path":"test/function.md","ref":"test/function.md","articles":[]},{"title":"非功能测试","level":"1.6.3","depth":2,"path":"test/noFunction.md","ref":"test/noFunction.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":["search-plus","-lunr","-search","tbfed-pagefooter","simple-page-toc","insert-logo","back-to-top-button","toggle-chapters","code","lightbox","copy-code-button","search-pro","emphasize","splitter","mermaid-gb3","-sharing","sharing-plus","prism","-highlight","flexible-alerts","page-toc-button","baidu","pageview-count","bilibili","donate","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy senvon 2020","modify_label":"Bilibili,头条号,微信号：请搜索  随senvon飞"},"prism":{},"emphasize":{},"livereload":{},"simple-page-toc":{"maxDepth":5,"skipFirstH1":true},"splitter":{},"search-pro":{},"bilibili":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"code":{"copyButtons":true},"donate":{"alipay":"images/toutiao.jpg","alipayText":"头条号","button":"联系方式","title":"联系方式","wechat":"images/weixin.PNG","wechatText":"公众号"},"fontsettings":{"theme":"white","family":"sans","size":2},"mermaid-gb3":{},"lightbox":{"jquery":true,"sameUuid":false},"page-toc-button":{"maxTocDepth":5,"minTocSize":2},"back-to-top-button":{},"pageview-count":{},"flexible-alerts":{"danger":{"className":"danger","icon":"fa fa-ban","label":"Attention"},"note":{"className":"info","icon":"fa fa-info-circle","label":"Note"},"style":"flat","tip":{"className":"tip","icon":"fa fa-lightbulb-o","label":"Tip"},"warning":{"className":"warning","icon":"fa fa-exclamation-triangle","label":"Warning"}},"copy-code-button":{},"baidu":{"token":"51550945f89f43511a44753249ab6675"},"sharing":{"qq":true,"all":["douban","qq","qzone","weibo"],"douban":false,"facebook":false,"weibo":true,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":false,"messenger":false,"line":false,"vk":false,"pocket":false,"google":false,"viber":false,"stumbleupon":false,"qzone":true,"linkedin":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"toggle-chapters":{},"insert-logo":{"style":"background: none; height: 170px; width: 200px","url":"images/logo3.jpg"},"search-plus":{}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"启路框架-使用手册","language":"zh-hans","backplugin":["lightbox"],"gitbook":"*","description":"启路框架-使用手册"},"file":{"path":"test/unitTest.md","mtime":"2021-10-08T06:51:27.988Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-10-19T02:57:27.717Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search-plus/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-plus/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-insert-logo/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-toggle-chapters/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lightbox/js/lightbox.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-copy-code-button/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-mermaid-gb3/book/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-flexible-alerts/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-page-toc-button/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-baidu/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    <script src="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.min.js"></script>

    </body>
</html>

